package indi.zhifa.recipe.bailan.busy.auth.controller.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.zhifa.recipe.bailan.framework.auth.impl.entity.dto.TokenObject;
import indi.zhifa.recipe.bailan.busy.auth.entity.dto.user.EditUserDto;
import indi.zhifa.recipe.bailan.busy.auth.entity.dto.user.SignInDto;
import indi.zhifa.recipe.bailan.framework.auth.impl.entity.enums.Gender;
import indi.zhifa.recipe.bailan.busy.auth.entity.vo.UserVo;
import indi.zhifa.recipe.bailan.busy.auth.service.IUserService;
import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.PasswordExpireAuth;
import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.auth.RequireRole;
import indi.zhifa.recipe.bailan.framework.auth.entity.annotations.auth.DataPermission;
import indi.zhifa.recipe.bailan.framework.auth.entity.dto.user.ChangePasswdDto;
import indi.zhifa.recipe.bailan.framework.auth.entity.enums.EDataPermissionType;
import indi.zhifa.recipe.bailan.framework.auth.entity.po.BaseUserEntity;
import indi.zhifa.recipe.bailan.framework.auth.impl.entity.po.UserEntity;
import indi.zhifa.recipe.bailan.framework.auth.impl.util.ITokenUtil;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.UnLogin;
import indi.zhifa.recipe.bailan.framework.common.entity.annotations.ZfRestController;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

@Api(tags = "UserApi-用户接口")
@RequestMapping("/api/user")
@Slf4j
@ZfRestController
@RequiredArgsConstructor
public class UserApi {

    private final IUserService mUserService;
    private final ITokenUtil mTokenUtil;

    @UnLogin
    @Operation(summary = "注册")
    @PostMapping(value = "/signin")
    public UserEntity signIn(
            @Parameter(description = "注册信息") @RequestBody SignInDto pSignInDto){
        UserEntity userEntity = mUserService.signIn(pSignInDto);
        return userEntity;
    }

    @RequireRole(roles = {"accountMgr","spAdm"})
    @DataPermission(domain = "zf-account", permissionType = EDataPermissionType.AUTH_CASCADE_CHILDREN)
    @Operation(summary = "修改信息-管理员")
    @PutMapping(value = "/{id}")
    public UserEntity edit(
            @Parameter(description = "用户Id") @PathVariable(name = "id") Long pId,
            @Parameter(description = "用户信息") @RequestBody EditUserDto pEditUserDto){
        UserEntity userEntity = mUserService.edit(pId,pEditUserDto);
        return userEntity;
    }

    @Operation(summary = "修改信息-自己")
    @PutMapping(value = "/self")
    public UserEntity editSelf(
            @Parameter(description = "用户信息") @RequestBody EditUserDto pEditUserDto){
        TokenObject tokenObject = mTokenUtil.getTokenObject();
        UserEntity userEntity = mUserService.edit(tokenObject.getId(),pEditUserDto);
        return userEntity;
    }


    @PasswordExpireAuth
    @Operation(summary = "修改密码")
    @PutMapping(value = "/passwd")
    public String changePasswdByUser(
            @Parameter(description = "密码信息") @RequestBody ChangePasswdDto pChangePasswdCfg
    ){
        BaseUserEntity baseUserEntity = mUserService.changePasswdByUser(pChangePasswdCfg);
        return "密码修改成功";
    }

    @DataPermission(domain = "zf-account")
    @RequireRole(roles = {"accountMgr","spAdm"})
    @Operation(summary = "重置密码")
    @PutMapping(value = "/{id}/resetPasswd")
    public String changePasswdByUser(
            @Parameter(description = "用户Id") @PathVariable(name = "id") Long pId){
        BaseUserEntity baseUserEntity = mUserService.resetPasswd(pId);
        return "密码修改成功";
    }

    @Operation(summary = "查看信息")
    @GetMapping(value = "/{id}")
    public UserVo info(
            @Parameter(description = "用户Id") @PathVariable(name = "id") Long pId){
        UserVo userVo = mUserService.info(pId);
        return userVo;
    }

    @Operation(summary = "查看分页")
    @GetMapping(value = "/page")
    public Page<UserVo> page(
            @Parameter(description = "当前页",required = true) @RequestParam(name = "current") Integer pCurrent,
            @Parameter(description = "页大小",required = true) @RequestParam(name = "size") Integer pSize,
            @Parameter(description = "权限码") @RequestParam(name = "roleCode", required = false) String pRoleCode,
            @Parameter(description = "部门Id") @RequestParam(name = "depId", required = false) Long pDepId,
            @Parameter(description = "部门级联码") @RequestParam(name = "depCascade", required = false) String pDepCascade,
            @Parameter(description = "用户名") @RequestParam(name = "userName", required = false) String pUserName,
            @Parameter(description = "邮箱") @RequestParam(name = "email", required = false) String pEmail,
            @Parameter(description = "电话") @RequestParam(name = "phone", required = false) String pPhone,
            @Parameter(description = "昵称") @RequestParam(name = "nickName", required = false) String pNickName,
            @Parameter(description = "性别") @RequestParam(name = "gender", required = false) Gender pGender,
            @Parameter(description = "最小年龄") @RequestParam(name = "ageMin", required = false) Integer pAgeMin,
            @Parameter(description = "最大年龄") @RequestParam(name = "ageMax", required = false) Integer pAgeMax

    ){
        Page<UserVo> pageInfo = mUserService.page(pCurrent,pSize,pRoleCode,pDepId,pDepCascade,pUserName,pEmail,pPhone,pNickName,pGender,pAgeMin,pAgeMax);
        return pageInfo;
    }

    @DataPermission(domain = "zf-account", permissionType = EDataPermissionType.AUTH_CASCADE_CHILDREN)
    @RequireRole(roles = {"accountMgr","spAdm"})
    @Operation(summary = "删除用户数据")
    @DeleteMapping(value = "/{id}")
    public String delete(@Parameter(description = "用户Id") @PathVariable(name = "id") Long pId){
        mUserService.removeUser(pId);
        return "删除成功";
    }

}
